//app.js
App({
  // 服务地址
  url: require('./config.js').Config['url'],

  // 全局参数
  globalData: {
    config: require('./config.js').Config,
    token: '',
    userInfo: null,
    wxappStyle: {}
  },

  // 工具类
  utils: require('./utils/util.js'),

  onLaunch: function(options) {
    // 是否是通过分享进入
    let sharerId = '';
    if (options.scene) {
      let scene = decodeURIComponent(options.query.scene);
      sharerId = scene.split('=')[1];
      console.log('=======');
      console.log(sharerId);
    }
    wx.setStorageSync('sharerId', sharerId);

    // 检查是否有版本更新
    this.utils.updateWxApp();

    // 第三方参数
    const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    console.log('=========getExtConfigSync获取第三方参数=========')
    console.log(extConfig);
    if (!extConfig.domain) {
      Object.assign(extConfig, {
        // 测试环境
        domain: 'https://service-emhatyrq-1251017581.gz.apigw.tencentcs.com/test/cars-pai',
        env: 'ws-rentcar-f8d2-test'
      });
    } else {
      this.url = extConfig['domain'] + '/car-tcf';
    }

    console.log('=========最终参数=========')
    console.log(extConfig);
    console.log(this.url);

    // 小程序云开发初始化
    // wx.cloud.init({
    //   // 云函数环境ID
    //   env: extConfig['env'],
    //   // 获取调用api者的信息
    //   traceUser: true,
    // });

    // 校验是否授权
    this.utils.isAuthorize(this, this.utils.initWxappStyle);
    // 初始化样式
    // this.utils.initWxappStyle(this);
  },

  // 页面初始化赋值
  pageOnloadInit(that, app) {
    if (app.globalData.wxappStyle) {
      app.initPage(that, app);
    } else {
      app.userInfoReadyCallback = function() {
        app.initPage(that, app);
      }
    }
  },

  // 执行初始化
  initPage(that, app) {
    const mainColor = app.globalData.wxappStyle.mainColor || wx.getStorageSync('wxappStyle').mainColor || '#fff';
    that.setData({
      "navbarShowMode": app.globalData.wxappStyle.navbarShowMode,
      "mainColor": mainColor,
      "url": app.url
    });
    // 设置顶部导航栏样式
    wx.setNavigationBarColor({
      frontColor: '#ffffff', // 字体颜色
      backgroundColor: mainColor, // 背景色
    });
    // 设置上拉下拉背景色
    wx.setBackgroundColor({
      backgroundColorTop: mainColor, // 顶部窗口的背景色为白色
      backgroundColorBottom: '#ffffff', // 底部窗口的背景色为白色
      complete: function(res) {
        console.log(res)
      }
    });

    var service = require('./utils/service.js');
    that.getFormId = function(e) {
      console.log('====addWxappFormId====');
      console.log(e.detail.formId);
      service.getConnect(service.api.addWxappFormId, {
        formId: e.detail.formId
      });
    };
    that.onShareAppMessage = function() {
      let wxappStyle = wx.getStorageSync('wxappStyle');
      let sharePath = 'pages/index/index';
      app.globalData.userInfo['id'] && (sharePath = sharePath + '?sharerId=' + app.globalData.userInfo['id']);
      console.log('===================');
      console.log(sharePath);
      return {
        title: app.globalData['wxappStyle']['wxappTitle'] || wxappStyle['wxappTitle'],
        path: sharePath
      }
    }
  },

  // 跳转页面
  jumpToPage: function(url, name = '') {
    // 当url为导航栏上的页面是需要进行判断
    wx.navigateTo({
      url: url + '?pageName=' + name
    })
  },

  // 弹窗
  showConfirm(title, content, cancel, cb, cb2, cancelText = '取消', confirmText = '确定') {
    wx.showModal({
      title: title,
      showCancel: cancel,
      cancelText: cancelText,
      content: content,
      confirmColor: this.globalData.wxappStyle.mainColor || wx.getStorageSync('wxappStyle').mainColor || '#19c5f4',
      confirmText: confirmText,
      success: function(res) {
        if (res.confirm) {
          typeof cb == "function" && cb();
        } else {
          typeof cb2 == "function" && cb2();
        }
      }
    })
  }
})